package org.example.top40;

import java.util.ArrayList;
import java.util.List;

public class main15 {
    //全排列
    //输入：nums = [1,2,3]
    //输出：[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
    static List<List<Integer>> res = new ArrayList<>();
    static List<Integer> path = new ArrayList<>();
    static boolean[] flag;

    public static void main(String[] args) {
        int[] nums = {1, 2, 3};
        flag = new boolean[nums.length];
        dfs(nums);
        System.out.println(res);
    }

    private static void dfs(int[] nums) {
        if (path.size() == nums.length) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (flag[i]){
                continue;
            }
            path.add(nums[i]);
            flag[i]=true;
            dfs(nums);
            flag[i]=false;
            path.remove(path.size()-1);
        }
    }
}